package com.shwread.android.qysw.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.NewActionHelp;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw10000136.R;
import com.shwread.android.ui.widget.LoadingView;
import com.shwread.http.base.ObjectHttpResponseHandler;
import com.shwread.http.util.Logger;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.List;

import logic.bean.SortInfo;
import logic.constants.DefaultConsts;
import logic.util.Utils;


public class GoodsSortActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener {
    private Context context;
    private TextView tvTitle;
    private Button butBack, butHome, btnHot;
    private LoadingView loadingView;

    private GridView gridView;
    private MyGridAdapter adapter;
    private List<SortInfo> sortDatas;
    private LinearLayout llBody, llayOtherView, llEmpty;
    private SortInfo hotData;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {

        }

        ;
    };

    @Override
    public Handler getUIHandler() {
        // TODO Auto-generated method stub
        return handler;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.goods_sort);
        context = this;

        findView();
        initLoadingView();
        getSort();
        addListener();
    }

    private void findView() {
        tvTitle = (TextView) findViewById(R.id.common_header_name);
        butBack = (Button) findViewById(R.id.common_header_back);
        butHome = (Button) findViewById(R.id.common_header_home);

        llBody = (LinearLayout) findViewById(R.id.ll_goods_sort_body);
        llEmpty = (LinearLayout) findViewById(R.id.ll_goods_sort_empty);
        loadingView = (LoadingView) findViewById(R.id.order_loading_view);
        llBody.setVisibility(View.GONE);
        llEmpty.setVisibility(View.GONE);
        loadingView.setVisibility(View.VISIBLE);

        btnHot = (Button) findViewById(R.id.goods_sort_hot_sale_keyword_button);
        gridView = (GridView) findViewById(R.id.goods_sort_other_gridv);
        llayOtherView = (LinearLayout) findViewById(R.id.goods_sort_other_llay_view);

        tvTitle.setText("分类");
        butBack.setVisibility(View.VISIBLE);
        butHome.setVisibility(View.VISIBLE);
        //近期热卖  View

        adapter = new MyGridAdapter();

    }

    private void initLoadingView() {
        loadingView.setLoadingAnim(R.drawable.home_progress);
        loadingView.changeLoadingStatus(0);
    }

    private void addListener() {
        butBack.setOnClickListener(this);
        butHome.setOnClickListener(this);
        btnHot.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    class MyGridAdapter
            extends
            BaseAdapter {

        @Override
        public int getCount() {
            return sortDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return sortDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Button otherBtn;

            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.goods_sort_other_btn, null);
                otherBtn = (Button) convertView.findViewById(R.id.goods_sort_other_btn);
                convertView.setTag(otherBtn);
            } else {
                otherBtn = (Button) convertView.getTag();
            }
            otherBtn.setText(sortDatas.get(position).getName());
            otherBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(GoodsSortActivity.this, GoodsBoxActivity.class);
                    intent.putExtra(DefaultConsts.FROM_TYPE, DefaultConsts.ECOMMERCE_TAG_FROM_SORT);
                    intent.putExtra(DefaultConsts.ECOMMERCE_SORT_TYPE, sortDatas.get(position).getItemId());
                    intent.putExtra(DefaultConsts.ECOMMERCE_SORT_NAME, sortDatas.get(position).getName());
                    startActivity(intent);
                }
            });

            return convertView;
        }

    }

    private void getSort() {

        NewActionHelp.qryGoodsItems(this, new ObjectHttpResponseHandler<List<SortInfo>>() {

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Logger.e(responseString);
                if (statusCode < 1000) {
                    Utils.ToastShow(context, "网络错误");
                } else {
                    Utils.ToastShow(context, "请求出错");
                }
                System.out.println("xxxxxxxxxxxxxxxx_onFailure" + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, List<SortInfo> object, String responseString) {
                loadingView.setVisibility(View.GONE);
                if (object != null && object.size() > 0) {
                    llBody.setVisibility(View.VISIBLE);
                    llEmpty.setVisibility(View.GONE);
                    hotData = object.get(0);
                    btnHot.setText(hotData.getName());
                    if (object.size() > 1) {
                        llayOtherView.setVisibility(View.VISIBLE);
                        object.remove(0);
                        sortDatas = object;
                        gridView.setAdapter(adapter);
                    } else {
                        llayOtherView.setVisibility(View.GONE);
                    }
                } else {
                    llBody.setVisibility(View.GONE);
                    llEmpty.setVisibility(View.VISIBLE);
                }
                System.out.println("xxxxxxxxxxxxxxxx_onSuccess" + responseString);
            }

            @Override
            public Type getType() {
                return new TypeToken<List<SortInfo>>() {
                }.getType();
            }

        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_header_back:
                finish();
                break;
            case R.id.common_header_home:
                //				Utils.showToast(bActivity, "您点击了主页面");
                //				finish();
                //				startActivity(new Intent(GoodsSortActivity.this, GoodsOrderDeliverDetailActivity.class));
//				startActivity(new Intent(GoodsSortActivity.this, MainActivity.class));
                BaseSimpleActivity.gotoMainActivity();
                break;
            case R.id.goods_sort_hot_sale_keyword_button:
                Intent intent = new Intent(GoodsSortActivity.this, GoodsBoxActivity.class);
                intent.putExtra(DefaultConsts.FROM_TYPE, DefaultConsts.ECOMMERCE_TAG_FROM_SORT);
                intent.putExtra(DefaultConsts.ECOMMERCE_SORT_TYPE, hotData.getItemId());
                intent.putExtra(DefaultConsts.ECOMMERCE_SORT_NAME, hotData.getName());
                startActivity(intent);
                break;
            default:
                break;
        }

    }

}
